package com.markul.android.ui.personal

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.MotionEvent
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.markul.android.R
import com.markul.android.logic.model.Repository
import com.markul.android.ui.UiTool
import com.markul.android.ui.detail.DetailedActivity
import com.markul.android.ui.mark.OnAblumItemClickListener
import kotlinx.android.synthetic.main.activity_mark.picRecycler
import kotlinx.android.synthetic.main.activity_personal.*

class PersonalActivity : AppCompatActivity(), OnAblumItemClickListener {

    private lateinit var ablumAdapter: AlbumAdapter
    var handler = Handler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_personal)
        UiTool.hideStatusNavigationBar(this, true)
        val viewModel: PersonalViewModel by viewModels()
        ablumAdapter = AlbumAdapter(viewModel.photosList, viewModel.photoNumber)
        initRecyclerview()
        val intent = Intent(this, BufferActivity::class.java)
        startActivity(intent)
        //填充个人相册
                Repository.allPhotos.observe(this, {
                    if (it.isNotEmpty()) {
                        viewModel.photosList.clear()
                        viewModel.photoNumber.clear()
                        viewModel.albumNumber.clear()
                        viewModel.albumId.clear()
                        viewModel.number = 0
                        spotText.text=it.size.toString()
                        for (i in it) {
                            if (i.album.albumTime.substring(0, 8) !in viewModel.photosList) {
                                viewModel.photosList.add(i.album.albumTime.substring(0, 8))
                                viewModel.photoNumber.add(0)
                                viewModel.albumNumber.add(0)
                                viewModel.albumId.add(0)
                            }
                            viewModel.photoNumber.add(i.photoList.size)
                            viewModel.photosList.add(i.photoList[0].photoUrl)
                            viewModel.albumNumber.add(viewModel.number)
                            viewModel.albumId.add(i.album.albumId)
                            viewModel.number++
                            ablumAdapter.notifyDataSetChanged()
                        }
                    }else{
                        viewModel.photosList.add("你的相册还是空空如也哦！快去打卡吧！")
                        ablumAdapter.notifyDataSetChanged()
                    }
                })
        backBtn.setOnClickListener {
            finish()
        }

    }

    private fun initRecyclerview() {
        picRecycler.layoutManager = GridLayoutManager(this, 3)
        picRecycler.adapter = ablumAdapter
        ablumAdapter.setOnMyClickListener(this)
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        when (ev.getAction()) {
            MotionEvent.ACTION_UP ->
                //设置延时让画面先加载好
                handler.postDelayed(
                    Runnable {
                        UiTool.hideStatusNavigationBar(this, true)
                    }, 3000
                )
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onPicsClick(position: Int) {
        val viewModel: PersonalViewModel by viewModels()
        val intent = Intent(this, DetailedActivity::class.java)
        intent.putExtra("number", "more")
        intent.putExtra("position", viewModel.albumNumber[position])
        intent.putExtra("id", viewModel.albumId[position])
        startActivity(intent)
    }

    override fun onPicOnlyClick(position: Int) {
        val viewModel: PersonalViewModel by viewModels()
        val intent = Intent(this, DetailedActivity::class.java)
        intent.putExtra("number", "only")
        intent.putExtra("position", viewModel.albumNumber[position])
        intent.putExtra("id", viewModel.albumId[position])
        startActivity(intent)
    }

}